package cn.controller;

import java.net.URLDecoder;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.kpi.entity.lawassessment.LawAssTemplateEntity;
import cn.kpi.entity.workassessment.WorkAssTemplateEntity;
import cn.kpi.service.lawassessment.ILawAssTemplateService;
import cn.kpi.service.workassessment.IWorkAssTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.common.ExceStatisResult;
import cn.common.LayTable;
import cn.filter.LoginCheck;
import cn.kpi.entity.sys.OrgEntity;
import cn.kpi.entity.sys.UserEntity;
import cn.kpi.entity.zhfx.WorkassGroupEntity;
import cn.kpi.entity.zhfx.WorkassTempEntity;
import cn.kpi.service.sys.IOrgService;
import cn.kpi.service.zhfx.IZhfxService;

@Controller
@RequestMapping("zhfx")
public class ZhfxController extends BaseController {
	
	@Autowired
	private IOrgService iOrgService;
	
	@Autowired
	private IZhfxService iZhfxService;

	@Autowired
	private IWorkAssTemplateService iWorkAssTemplateService;

	@Autowired
	private ILawAssTemplateService iLawAssTemplateService;


	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_tempalte_list")
	public LayTable<LawAssTemplateEntity> GetList(HttpServletRequest request) {

		List<LawAssTemplateEntity> lst = null;
		LayTable<LawAssTemplateEntity> layTable = new LayTable<LawAssTemplateEntity>();
		try {
			String templatename = request.getParameter("templatename");

			String where = "";
			if (templatename != null && templatename.length() > 0) {
				where += " AND templatename LIKE '%" + templatename.trim() + "%'";
			}

			lst = iLawAssTemplateService.GetLawAssTemplateListByWhere(where);
			for (int i = 0; i < lst.size(); i++) {
				lst.get(i).setRowNum(i + 1);
			}

			int iCount = iLawAssTemplateService.GetLawAssTemplateListCountByWhere(where);
			layTable.setCode(0);
			layTable.setCount(iCount);
			layTable.setData(lst);

		} catch (Exception e) {
			layTable.setCode(500);
		}
		return layTable;
	}

	@RequestMapping(value = "/bmfx_list")
	public String BmfxList(Model model) {
		try {

		} catch (Exception ex) {

		}
		return "zhfx/bmfx_list";
	}


	@RequestMapping(value = "/ryqkfx_list")
	public String RyqkfxList(Model model) {
		try {
			Calendar cale = Calendar.getInstance();          
			int year = cale.get(Calendar.YEAR);          
			int month = cale.get(Calendar.MONTH);
			/*if(month==0){
				year=year-1;
				month=12;
			}*/
			UserEntity user=this.GetLoginUser();
			model.addAttribute("year", year);
			model.addAttribute("month", month+1);
			model.addAttribute("szdw", user.getSzdw());
			model.addAttribute("orgname", user.getOrgname());
		} catch (Exception ex) {

		}

		return "zhfx/ryqkfx_list";
	}
	@RequestMapping(value = "/zfkh_list")
	public String ZfkhList(Model model) {
		try {

			Calendar cale = Calendar.getInstance();          
			int year = cale.get(Calendar.YEAR);          
			int month = cale.get(Calendar.MONTH);
			if(month==0){
				year=year-1;
				month=12;
			}
			UserEntity user=this.GetLoginUser();
			model.addAttribute("year", year);
			model.addAttribute("month", month);
			model.addAttribute("szdw", user.getSzdw());
			model.addAttribute("orgname", user.getOrgname());
			
		} catch (Exception ex) {

		}

		return "zhfx/zfkh_list";
	}
	
	@RequestMapping(value = "/dwkh_list")
	public String DwkhList(Model model) {
		try {
			Calendar cale = Calendar.getInstance();          
			int year = cale.get(Calendar.YEAR);          
			int month = cale.get(Calendar.MONTH);

			int quarter = 1;
			switch (month+1) {
				case 1:
				case 2:
				case 3:
					quarter = 1;
					break;
				case 4:
				case 5:
				case 6:
					quarter = 2;
					break;
				case 7:
				case 8:
				case 9:
					quarter = 3;
					break;
				case 10:
				case 11:
				case 12:
					quarter = 4;
					break;
			}
			if (quarter == 1) {
				quarter=4;
			} else {
				quarter = quarter - 1;
			}
			if(month==0){
				month=12;
				year =year -1;
			}


			UserEntity user=this.GetLoginUser();
			model.addAttribute("year", year);
			model.addAttribute("month", month);//月份就需要默认为上个月
			model.addAttribute("quarter",quarter);
			model.addAttribute("szdw", user.getSzdw());
			model.addAttribute("orgname", user.getOrgname());
			
		} catch (Exception ex) {

		}

		return "zhfx/dwkh_list";
	}
	
	@RequestMapping(value = "/ywkhfx_list")
	public String YwkhfxList(Model model) {
		try {

			Calendar cale = Calendar.getInstance();          
			int year = cale.get(Calendar.YEAR);          
			int month = cale.get(Calendar.MONTH);
			/*if(month==0){
				year=year-1;
				month=12;
			}*/
			UserEntity user=this.GetLoginUser();
			model.addAttribute("year", year);
			model.addAttribute("month", month+1);
			model.addAttribute("szdw", user.getSzdw());
			model.addAttribute("orgname", user.getOrgname());
			
			 List<WorkassTempEntity> templist=iZhfxService.GetWorkassTempList();
			 model.addAttribute("templist", templist);
			 
			 List<WorkassGroupEntity> grouplist=iZhfxService.GetWorkassGroupList();
			 model.addAttribute("grouplist", grouplist);
			
		} catch (Exception ex) {

		}

		return "zhfx/ywkhfx_list";
	}
	
	
	@RequestMapping(value = "/ryqkfx_user_detail")
	public String UserDetail(Model model,String szdw,int flag,String xname) {
		try {
			model.addAttribute("szdw", szdw);
			model.addAttribute("flag", flag);
			model.addAttribute("xname", xname);
		} catch (Exception ex) {

		}

		return "zhfx/ryqkfx_user_detail";
	}
	
	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_list_plyymb")
	public LayTable<UserEntity> GetListPlyymb(HttpServletRequest request) {

		List<UserEntity> lst = null;
		LayTable<UserEntity> layTable = new LayTable<UserEntity>();
		try {
			String realname = request.getParameter("realname");
			String szdw = request.getParameter("szdw");
			String flag = request.getParameter("flag");
			String xname = request.getParameter("xname");
			
			String where = "";
		
			if (realname != null && realname.length() > 0) {
				where += " AND realname LIKE '%" + realname + "%'";
			}
			if (szdw != null && szdw.length() > 0 && !szdw.equals("0")) {
				OrgEntity orgentity = iOrgService.GetOrgById(Long.parseLong(szdw));
				where += " AND szdw IN (SELECT orgid FROM t_sys_org WHERE lft>=" + orgentity.getLft() + " AND lft<="
						+ orgentity.getRgt() + ")";
			}
			else{
				where+=this.GetUserListByRole();
			}
			
			
			//where+=this.GetUserListByRole();
			
//			int page=Integer.parseInt(request.getParameter("page"));
//			int limit=Integer.parseInt(request.getParameter("limit"));
//			int beginrow=(page-1)*limit;
//			int endrow=limit;
			
			
			
			//String limitwhere=where+" LIMIT "+beginrow+","+endrow;
			
			switch(flag){
			case "1"://年龄
				int age=Integer.parseInt(xname);
				where+=" AND (YEAR(NOW())-YEAR(birthday))<="+age+" AND (YEAR(NOW())-YEAR(birthday))>"+(age-5);
				break;
			case "2"://党员
				int agedy=Integer.parseInt(xname);
				where+=" AND (YEAR(NOW())-YEAR(birthday))<="+agedy+" AND (YEAR(NOW())-YEAR(birthday))>"+(agedy-5)+" AND (zzmm='中共党员' OR zzmm='中共预备党员')";
				break;
			case "3"://学历
				String xl =URLDecoder.decode(xname,"utf-8");
				if (xl.equals("初中")) {
					xl = "初中毕业";
				}
				if (xl.equals("高中")) {
					xl = "高中毕业";
				}
				if (xl.equals("中专")) {
					xl = "中专毕业";
				}
				if (xl.equals("职高")) {
					xl = "职高毕业";
				}
				if (xl.equals("专科")) {
					xl = "专科毕业";
				}
				if (xl.equals("本科")) {
					xl = "大学毕业";
				}
				if (xl.equals("硕士")) {
					xl = "硕士研究生";
				}
				if (xl.equals("博士")) {
					xl = "博士研究生";
				}
				where += " AND qualifications='" + xl + "'";
				break;
			}
			
			lst = iZhfxService.GetUserListByWhere(where);
			for (int i = 0; i < lst.size(); i++) {
				int numr=(i + 1);
				lst.get(i).setRowNum(numr);
			}

			//int iCount = iUserUsAssTempService.GetUserUsAssTempListCountByWhere(where);
			int iCount = 0;
			layTable.setCode(0);
			layTable.setCount(iCount);
			layTable.setData(lst);

		} catch (Exception e) {
			e.printStackTrace();
			layTable.setCode(500);
		}
		return layTable;
	}

	@ResponseBody
	@RequestMapping(value = "/getzfkhdata")
	public ExceStatisResult<Map<String, List<Map>>> GetzfkhData(HttpServletRequest request,
			HttpServletResponse response) {
		ExceStatisResult<Map<String, List<Map>>> result = new ExceStatisResult<Map<String, List<Map>>>();
		result.setCode(400);
		result.setResult(0);
		try {

			Long templateId = iLawAssTemplateService.GetLawAssTemplateListByWhere("").get(0).getTemplateid();
			String templatestr = request.getParameter("templateid");
			if(templatestr!=null&&templatestr.length()>0) {
				templateId = Long.valueOf(templatestr);
			}
			String szdw = request.getParameter("szdw");
			String where = "";
			List<Map> monthmap = iZhfxService.GetZfkhTemplateList(where, templateId);

			//排序
			List<Float> list = new ArrayList();
			Map<Float,Integer> orderMap = new HashMap<>();
			for (Map map : monthmap) {
			   Float score = Float.valueOf(map.get("score").toString());
			   list.add(score);
			}
			Collections.sort(list);
			Collections.reverse(list);
			int count =0;
			for ( Float it : list) {
				if(!orderMap.containsKey(it))
					orderMap.put(it,++count);
			}

			for (Map map : monthmap) {
				Float score = Float.valueOf(map.get("score").toString());
				Integer top = orderMap.get(score);
				map.put("top",top);
			}


			if(szdw==null||szdw.length()<=0){
				UserEntity user=this.GetLoginUser();
				szdw=user.getSzdw()+"";
			}
			String where1=" AND orgid="+szdw;
			//todo:先写死2018
			List<Map> yearmap = iZhfxService.GetZfkhList(where1, 2018, 0);
			Map<String, List<Map>> data = new HashMap<String, List<Map>>();
			data.put("monthmap", monthmap);
			data.put("yearmap", yearmap);
			result.setCode(200);
			result.setResult(1);
			result.setMsg("操作成功");
			result.setItems(data);
		} catch (Exception ex) {
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/getdwkhdata")
	public ExceStatisResult<Map<String, List<Map>>> GetDwkhData(HttpServletRequest request,
			HttpServletResponse response) {
		ExceStatisResult<Map<String, List<Map>>> result = new ExceStatisResult<Map<String, List<Map>>>();
		result.setCode(400);
		result.setResult(0);
		try {
			String year = request.getParameter("year");
			String month = request.getParameter("month");
			String quarter = request.getParameter("quarter");
			String type = request.getParameter("type");
			String szdw = request.getParameter("szdw");

			int divisor = 1;
			//根据类型筛选条件
			if (type.equals("y")) {
				month="0";
				quarter="0";
				divisor = 12;
			} else if (type.equals("m")) {
				quarter="0";
			} else {
				month="0";
				divisor = 3;
			}

			
			String where = "";
			List<Map> znmap = iZhfxService.GetDwkhList(where, Integer.parseInt(year), Integer.parseInt(month),Integer.valueOf(quarter),divisor,46);//职能部门
			List<Map> jczmap = iZhfxService.GetDwkhList(where, Integer.parseInt(year), Integer.parseInt(month),Integer.valueOf(quarter),divisor,47);//检查站
			List<Map> pcsmap = iZhfxService.GetDwkhList(where, Integer.parseInt(year), Integer.parseInt(month),Integer.valueOf(quarter),divisor,48);//派出所

			if(szdw==null||szdw.length()<=0){
				UserEntity user=this.GetLoginUser();
				szdw=user.getSzdw()+"";
			}
			String where1=" AND orgid="+szdw;
			List<Map> dwmap = iZhfxService.GetDwkhList(where1, Integer.parseInt(year), 0,0,12,0);
			Map<String, List<Map>> data = new HashMap<String, List<Map>>();
			data.put("znmap", znmap);
			data.put("jczmap", jczmap);
			data.put("pcsmap", pcsmap);
			data.put("dwmap", dwmap);
			result.setCode(200);
			result.setResult(1);
			result.setMsg("操作成功");
			result.setItems(data);
		} catch (Exception ex) {
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/getdwkhdatadw")
	public ExceStatisResult<Map<String, List<Map>>> GetDwkhDataDw(HttpServletRequest request,
			HttpServletResponse response) {
		ExceStatisResult<Map<String, List<Map>>> result = new ExceStatisResult<Map<String, List<Map>>>();
		result.setCode(400);
		result.setResult(0);
		try {
			String year = request.getParameter("year");
			String month = request.getParameter("month");
			String szdw = request.getParameter("szdw");
			
			if(szdw==null||szdw.length()<=0){
				UserEntity user=this.GetLoginUser();
				szdw=user.getSzdw()+"";
			}
			String where1=" AND orgid="+szdw;
			List<Map> dwmap = iZhfxService.GetDwkhdataList(where1, Integer.parseInt(year),0,0);
			Map<String, List<Map>> data = new HashMap<String, List<Map>>();

			data.put("dwmap", dwmap);
			result.setCode(200);
			result.setResult(1);
			result.setMsg("操作成功");
			result.setItems(data);
		} catch (Exception ex) {
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/getywkhdata")
	public ExceStatisResult<Map<String, List<Map>>> GetYwkhData(HttpServletRequest request,
			HttpServletResponse response) {
		ExceStatisResult<Map<String, List<Map>>> result = new ExceStatisResult<Map<String, List<Map>>>();
		result.setCode(400);
		result.setResult(0);
		try {
			
			String tempid = request.getParameter("tempid");
			
			String where="";
			if(tempid!=null&&tempid.length()>0){
				where+=" AND  templateid="+tempid;
			}
			else {
				tempid =iZhfxService.GetWorkassTempList().get(0).getTemplateid().toString();
				where+=" AND  templateid="+tempid;
			}
			Calendar cale = Calendar.getInstance();          
			int year = cale.get(Calendar.YEAR);          
			int month = cale.get(Calendar.MONTH)+1;
			List<Map> ywmap = iZhfxService.GetYwkhList(where, year, month);
			List<Map> groupmap = iZhfxService.GetWorkassGroupMap();
			Map<String, List<Map>> data = new HashMap<String, List<Map>>();

			data.put("ywmap", ywmap);
			data.put("groupmap", groupmap);
			result.setCode(200);
			result.setResult(1);
			result.setMsg("操作成功");
			result.setItems(data);
		} catch (Exception ex) {
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/getywkhdatadw")
	public ExceStatisResult<Map<String, List<Map>>> GetYwkhDataDw(HttpServletRequest request,
			HttpServletResponse response) {
		ExceStatisResult<Map<String, List<Map>>> result = new ExceStatisResult<Map<String, List<Map>>>();
		result.setCode(400);
		result.setResult(0);
		try {
			
			String szdw = request.getParameter("szdw");
			
			
			
			Calendar cale = Calendar.getInstance();          
			int year = cale.get(Calendar.YEAR);          
			int month = cale.get(Calendar.MONTH)+1;
			
			if(szdw==null||szdw.length()<=0){
				UserEntity user=this.GetLoginUser();
				szdw=user.getSzdw()+"";
			}
			String where=" AND orgid="+szdw;
			
			List<Map> ywmapdw = iZhfxService.GetYwkhMonthListByOrgId(where, year);
			
			Map<String, List<Map>> data = new HashMap<String, List<Map>>();

			data.put("ywmapdw", ywmapdw);
			result.setCode(200);
			result.setResult(1);
			result.setMsg("操作成功");
			result.setItems(data);
		} catch (Exception ex) {
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}


	@ResponseBody
	@RequestMapping(value = "/getywkhyeardw")
	public ExceStatisResult<Map<String, List<Map>>> GetYwkhYearDw(HttpServletRequest request,
																  HttpServletResponse response) {
		ExceStatisResult<Map<String, List<Map>>> result = new ExceStatisResult<Map<String, List<Map>>>();
		result.setCode(400);
		result.setResult(0);
		try {

			String szdw = request.getParameter("szdw");
			String tempid = request.getParameter("tempid");


			Calendar cale = Calendar.getInstance();
			int year = cale.get(Calendar.YEAR);

			if(szdw==null||szdw.length()<=0){
				UserEntity user=this.GetLoginUser();
				szdw=user.getSzdw()+"";
			}


			List<Map> ywmapdw = iZhfxService.GetYwYearListByOrgId(tempid, szdw);

			Map<String, List<Map>> data = new HashMap<String, List<Map>>();

			data.put("ywmapdw", ywmapdw);
			result.setCode(200);
			result.setResult(1);
			result.setMsg("操作成功");
			result.setItems(data);
		} catch (Exception ex) {
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/getzfkhyeardw")
	public ExceStatisResult<Map<String, List<Map>>> GetZfkhYearDw(HttpServletRequest request,
																  HttpServletResponse response) {
		ExceStatisResult<Map<String, List<Map>>> result = new ExceStatisResult<Map<String, List<Map>>>();
		result.setCode(400);
		result.setResult(0);
		try {

			String szdw = request.getParameter("szdw");



			Calendar cale = Calendar.getInstance();
			int year = cale.get(Calendar.YEAR);

			if(szdw==null||szdw.length()<=0){
				UserEntity user=this.GetLoginUser();
				szdw=user.getSzdw()+"";
			}


			List<Map> zfmapdw = iZhfxService.GetZfYearListByOrgId(szdw);

			Map<String, List<Map>> data = new HashMap<String, List<Map>>();

			data.put("zfmapdw", zfmapdw);
			result.setCode(200);
			result.setResult(1);
			result.setMsg("操作成功");
			result.setItems(data);
		} catch (Exception ex) {
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/getdwkhyeardw")
	public ExceStatisResult<Map<String, List<Map>>> GetDwkhYearDw(HttpServletRequest request,
																  HttpServletResponse response) {
		ExceStatisResult<Map<String, List<Map>>> result = new ExceStatisResult<Map<String, List<Map>>>();
		result.setCode(400);
		result.setResult(0);
		try {

			String szdw = request.getParameter("szdw");



			Calendar cale = Calendar.getInstance();
			int year = cale.get(Calendar.YEAR);

			if(szdw==null||szdw.length()<=0){
				UserEntity user=this.GetLoginUser();
				szdw=user.getSzdw()+"";
			}


			List<Map> dwmapdw = iZhfxService.GetDwYearListByOrgId(szdw);

			Map<String, List<Map>> data = new HashMap<String, List<Map>>();

			data.put("dwmapdw", dwmapdw);
			result.setCode(200);
			result.setResult(1);
			result.setMsg("操作成功");
			result.setItems(data);
		} catch (Exception ex) {
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
}
